/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trojkaty;

import javax.swing.JComponent;
import wej.TDaneWej;
import gra.*;
import out.*;
import ste.*;

import javax.vecmath.*; //do grafiki
import java.util.*;

/**
 *
 * @author arkadiusz
 */
public class Trojkaty extends TAbstBlock {

    private final static double MIN_DIST = 0.0001; //minimalna odl.miedzyPktami
    private JComponent panel = new PanelTrojkaty(this);
    private int nrSkanu = 0;
    private Point3d[] thePopT; //tablica z poprzednimi odczytami
    private ArrayList<Tri> theTriAL = new ArrayList<Tri>(); //lista trojkatow

    @Override
    public String getTabTitle() {
        return "Nowe trojkaty";
    }

    @Override
    public JComponent getJComponent() {
        return this.panel;
    }

    @Override
    public void setNext(TAbstBlock aNext) { theNext = aNext; }

    @Override
    public void checkSettings() {
        theNext.checkSettings();
    }

    @Override
    public void showGraphic() {
        System.out.println( "showGraphics, ile trojkatow: " + theTriAL.size() );
        //throw new UnsupportedOperationException("Not supported yet.");
        TVisualisation _lVis = new TVisualisation(); //do grafiki
        ArrayList<Point3d> lPtAL  = new ArrayList<Point3d>();
        int i = 0;
        long memoroMB = 0;
        try {
            for (Tri t : theTriAL) {
                if(i%10000==0) {
                    //System.out.print("Czyszcenie pamieci...");
                    Runtime.getRuntime().gc();
                    memoroMB =  Runtime.getRuntime().freeMemory()/1024/1024;
                    System.out.println("("+i+") zrobione  "+ memoroMB +" MB");
                    if(memoroMB < 200){
                        System.out.println("BRAK PAMIECI!!!  ");
                        System.out.print("KONIEC " + (Runtime.getRuntime().maxMemory()/1024/1204-memoroMB) + "MB");
                        System.exit(0);
                        break;
                    }
                }
                i++;
                lPtAL.add(t.getSirodek());
                //if(i%10==0)
                  // _lVis.addPtT( new Point3d[]{t.getSirodek()});
                
                   //, t.getColor());
                   //_lVis.addTriangleT(t.getPoints3d(), t.getColor());
                //System.out.println("#");
            //_lVis.addTriangleT(t.getPoints3d(), t.getColor());
           
            }//for
        } catch (java.lang.OutOfMemoryError err) {
            System.out.println("KONIEC OutOfMemoryError: " + err + ", i: " + i);
            Runtime.getRuntime().gc();
            //err.printStackTrace();
            System.exit(0);
            // _lVis.display();
        }
        //System.out.print(Runtime.getRuntime().maxMemory()/1024/1204-memoroMB + "MB");
        //Point3d[] p3T = new Point3d[lPtAL.size()];
        //p3T = lPtAL.toArray(p3T);
        
        _lVis.addTriangleT(theTriAL.toArray(new Tri[theTriAL.size()]));
        _lVis.display();

    } //koniec showGraphic

    @Override
    public void clear() {
        thePopT = null;
        theTriAL.clear();
        theNext.clear();
    } //koniec clear

    @Override
    public void starter(IOut o) {
        if (!(o instanceof TOut1_2)) {
            return;
        }
        TOut1_2 in = (TOut1_2) o;

        //Zabezpieczenie.
        if (in.getXT().length != TDaneWej.ILE_LASER) {
            System.out.println("nrskanu: " + nrSkanu 
                    + ", theTriA.s: " + theTriAL.size() 
                    + ", in.getXT.l: " + in.getXT().length);
            System.exit(0);
        } //koniec if

        Point3d[] lNoweT = new Point3d[TDaneWej.ILE_LASER];

        //wpisuje do lNoweT wartoĹ›ci, z in
        for (int i = 0; i < lNoweT.length; i++) {
            lNoweT[i] = new Point3d(in.getXT()[i], in.getYT()[i], in.getZT()[i]);
        }

        if (nrSkanu > 0) { //cz. nrSkanu niepierwszy

            //Tworze trojkaty
            Point3d p1 = null, p2 = null, p3 = null, p4 = null;
//            for (int i = 1; i < lNoweT.length; i++) {
//                p1 = lNoweT[i - 1];
//                p2 = thePopT[i - 1];
            for (int i = 1; i < lNoweT.length; i++) {
                p1 = lNoweT[i - 1];
                p2 = thePopT[i - 1];
                p3 = thePopT[i];
                p4 = lNoweT[i];

                addTrojkat(theTriAL, p1, p2, p3);
                addTrojkat(theTriAL, p1, p3, p4);
            }  //koniec if - else
        } //koniec if
        nrSkanu++;

        //zapamientujemy ostatni skan
        thePopT = lNoweT;
//System.out.println("koniec starter, nrskanu: " + nrSkanu + ", theTriA.s: " + theTriAL.size());
        theNext.starter(o); //przesyłam to samo, co dostałem w argumencie
    } //koniec starter
    
    public void finish() {theNext.finish();}

    private void addTrojkat(ArrayList<Tri> aTriAL, Point3d pA, Point3d pB, Point3d pC) {
        /*
        //Spr. czy wszystkie pkty sa rozne
        if (MIN_DIST < pA.distance(pB) &&
        MIN_DIST < pB.distance(pC) &&
        MIN_DIST < pC.distance(pA) ){//&&
        //(pA.distance(pC) < prog || pB.distance(pC) < prog)) {
        aTriAL.add(new Tri(pA, pB, pC));
        }
         */
        //Spr. czy żaden z punktów nie ma jakieś wsp. w nieskonczonosci
        if (pA.getX()!=TDaneWej.MAX_VAL && pA.getY()!=TDaneWej.MAX_VAL && pA.getZ()!=TDaneWej.MAX_VAL &&
            pB.getX()!=TDaneWej.MAX_VAL && pB.getY()!=TDaneWej.MAX_VAL && pB.getZ()!=TDaneWej.MAX_VAL &&
            pC.getX()!=TDaneWej.MAX_VAL && pC.getY()!=TDaneWej.MAX_VAL && pC.getZ()!=TDaneWej.MAX_VAL)
            
            aTriAL.add(new Tri(pA, pB, pC));
    } //koniec addTrojkat
} //konic klasy Trojkaty

